package lablayer.view;

import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.Panel;
import javafx.scene.paint.Color;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.*;
import javafx.scene.control.Label;
import javafx.scene.control.TextBox;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import lablayer.model.ComponentFactory;
import lablayer.model.MathComponents.MathComponentFactory;
import lablayer.controller.UpdaterFactory;
import lablayer.view.state.StateFactory;
import lablayer.view.state.StateInterface;
import lablayer.view.stat.ViewLinkComponentBuffer;
import javafx.scene.layout.HBox;
import javafx.scene.control.Button;
import lablayer.model.LabData;
import java.lang.NumberFormatException;
import lablayer.view.stat.ViewBoxBuffer;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.lang.UnsupportedOperationException;

/**
 * @author Darkness
 */

public class ViewDotStudent extends ViewComponent {
    var lResult: Label;
    var panel: Panel;
	var signValueTextBox: TextBox;
	var freedomDegTextBox: TextBox;
	
    init {
        this.component = ComponentFactory.createComponent(MathComponentFactory.
            createDotStudent());
        this.component.setUpdater(UpdaterFactory.createUpdater(this));
    }

    override public function create(): Node {
        var vbox: VBox;        
        
        panel = Panel {
            layoutX: 0
            layoutY: 0
            width: 250
            height: 200

            content: [
                //background
                Rectangle {
                    layoutX: 0
                    layoutY: 0
                    width: bind panel.width
                    height: bind panel.height
                    fill: Color.web("#FFFFFF")
                    arcWidth: 15
                    arcHeight: 15
                    stroke: Color.web("#59649C")
                    strokeWidth: 1
                },
                vbox = VBox {
                    var startX: Float;
                    var startY: Float;
                    var tbData: TextBox;

                    layoutX: 5
                    layoutY: 5
                    width: bind (panel.width - 10)
                    height: bind (panel.height - 20)

                    content: [
                        Group {
                            layoutX: 0
                            layoutY: 0
                            content: [
                                //header
                                Rectangle {
                                    layoutX: 0
                                    layoutY: 0
                                    width: bind vbox.width
                                    height: 20
                                    fill: LinearGradient {
                                        startX: 0, startY: 0, endX: 225, endY: 0
                                        proportional: false
                                        stops: [
                                            Stop { offset: 0.0 color: Color.web("#5277BA") }
                                            Stop { offset: 1.0 color: Color.web("#4969A6") },
                                        ]
                                    }
                                    arcWidth: 10
                                    arcHeight: 10

                                    onMousePressed: function(e: MouseEvent): Void {
                                        if (StateFactory.getState().getStateId() == StateInterface.COMPONENT_STATE) {
											startX = this.translateX;
											startY = this.translateY;
											this.toFront();
										}
                                    }
                                    onMouseDragged: function(e: MouseEvent): Void {                                        
										if (StateFactory.getState().getStateId() == StateInterface.COMPONENT_STATE) {
											this.translateX = startX + e.dragX;
											this.translateY = startY + e.dragY;
										}
                                    }
                                },
                                Text {
                                    layoutX: 5
                                    layoutY: 14
                                    content: this.component.getComponentName()
                                    fill: Color.web("#FFFFFF")
                                }
                            ]
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
							// increase vbox width for long component name
                            width: bind (vbox.width + 40)
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        Label {
                            text: "Уровень значимости"
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        signValueTextBox = TextBox {
                            text: ""
                            columns: 12
                            selectOnFocus: false
                        },
                        Label {
                            text: "Число степеней свободы"
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        freedomDegTextBox = TextBox {
                            text: ""
                            columns: 12
                            selectOnFocus: false
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        Label {
                            text: "Значение"
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        lResult = Label {
                            text: ""
                            textFill: Color.web("#252C4F")
                        },
                        Rectangle {
                            layoutX: 0
                            layoutY: 0
                            width: bind vbox.width
                            height: 5
                            fill: Color.TRANSPARENT
                        },
                        HBox {
                            content: [
                                Button {
                                    text: "Принять"
                                    onMouseClicked: function(e: MouseEvent): Void {
                                        try {
                                            var data: Double[];
                                            insert Double.parseDouble(freedomDegTextBox.text) into data;
                                            insert Double.parseDouble(signValueTextBox.text) into data;
                                            this.component.setData(new LabData(data));
                                        } catch (ex: NumberFormatException) {}
                                        this.component.calculate();
                                    }
                                },
								Rectangle {
						            width: 5
								    height: 5
		                            fill: Color.TRANSPARENT
				                },
								Button {
                                    text: "Закрыть"
                                    onMouseClicked: function(e: MouseEvent): Void {
                                        if (StateFactory.getState().getStateId() == StateInterface.COMPONENT_STATE) {
											ViewBoxBuffer.getViewBox().removeViewComponent(this);
										}
                                    }
                                }
                            ]
                        }
                    ]
                }
            ]

            onMouseEntered: function(e: MouseEvent): Void {
                if (StateFactory.getState().getStateId() == StateInterface.LINK_STATE) {
                    ViewLinkComponentBuffer.setViewComponent(this);
                }
            }
            onMouseExited: function(e: MouseEvent): Void {
                if (StateFactory.getState().getStateId() == StateInterface.LINK_STATE) {
                    ViewLinkComponentBuffer.clear();
                }
            }
        }
        }

    override public function update () : Void {
        this.lResult.text = this.component.getResult().toString();
    }

	override public function saveToXML(): String {
		var xml: String = "";
		xml += "	<ViewComponent>\r\n";
		xml += "		<Type>{this.component.getComponentId()}</Type>\r\n";
		xml += "		<TranslateX>{this.translateX}</TranslateX>\r\n";
		xml += "		<TranslateY>{this.translateY}</TranslateY>\r\n";
		xml += "		<ViewComponentId>{this.getViewComponentId()}</ViewComponentId>\r\n";
		xml += "		<SignValue>{this.signValueTextBox.text}</SignValue>\r\n";
		xml += "		<FreedomDegree>{this.freedomDegTextBox.text}</FreedomDegree>\r\n";
		if (this.component != null) {
			xml += "		<Component>\r\n";
			var labData: LabData;
			try {
				labData = this.component.getInitData();
				xml += "			<InitData>{labData.toString()}</InitData>\r\n";
			} catch (e: UnsupportedOperationException) {}
			try {
				labData = this.component.getData();
				xml += "			<Data>{labData.toString()}</Data>\r\n";
			} catch (e: UnsupportedOperationException) {}
			try {
				labData = this.component.getResult();
				xml += "			<Result>{labData.toString()}</Result>\r\n";
			} catch (e: UnsupportedOperationException) {}
			xml += "		</Component>\r\n"; //<Component ... />
			xml += "	</ViewComponent>\r\n";
		} else {
			xml += "	</ViewComponent>\r\n";
		}
		return xml;
	}

	override public function loadFromXML(vcNode: org.w3c.dom.Node): Void {
		if (vcNode != null) {
			if (vcNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
				var vcElement: Element = vcNode as Element;
				var nodeList: NodeList;
				var domElement: Element;

				// get translateX
				nodeList = vcElement.getElementsByTagName("TranslateX");
				domElement = nodeList.item(0) as Element;
				this.translateX = Float.parseFloat(domElement.getChildNodes().
					item(0).getNodeValue());

				// get translateY
				nodeList = vcElement.getElementsByTagName("TranslateY");
				domElement = nodeList.item(0) as Element;
				this.translateY = Float.parseFloat(domElement.getChildNodes().
					item(0).getNodeValue());

				// get viewComponentId
				nodeList = vcElement.getElementsByTagName("ViewComponentId");
				domElement = nodeList.item(0) as Element;
				this.viewComponentId = domElement.getChildNodes().item(0).getNodeValue();

				// get SignValue
				nodeList = vcElement.getElementsByTagName("SignValue");
				domElement = nodeList.item(0) as Element;
				this.signValueTextBox.text = domElement.getChildNodes().
					item(0).getNodeValue();

				// get FreedomDegree
				nodeList = vcElement.getElementsByTagName("FreedomDegree");
				domElement = nodeList.item(0) as Element;
				this.freedomDegTextBox.text = domElement.getChildNodes().
					item(0).getNodeValue();

				// digging Component...
				nodeList = vcElement.getElementsByTagName("Component");
				// get Data
				domElement = nodeList.item(0) as Element;
				nodeList = domElement.getElementsByTagName("Data");
				domElement = nodeList.item(0) as Element;
				this.component.setData(new LabData(LabData.parseString(domElement.
					getChildNodes().item(0).getNodeValue())));
			}
		}
	}

}
